package eg.com.itiga.ui.actions.graphs;

import java.awt.image.BufferedImage;
import java.util.HashMap;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.JFreeChart;

import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.servlet.ServletUtilities;

import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;



import eg.com.itiga.business.factory.BusinessFactory;
import eg.com.itiga.business.report.ReportService;



public class ItianCompanyGraph extends org.apache.struts.action.Action {
    

	String[] companiesNameList;
	double[] companiesWorkingNumber;
	HashMap<String, Integer> companiesList;
	
	
	

   public ItianCompanyGraph() {
    }
    
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        // TODO: Write method body
    	
    	
    	
		try {
			ReportService g=BusinessFactory.createReportService();
			companiesList=g.getCompaniesAndItianWorkedFor();
			
			
			companiesNameList=new String[companiesList.size()];	
			companiesWorkingNumber=new double[companiesList.size()];
			
			int i=0;
			for(String str : companiesList.keySet())
			{
				companiesNameList[i]=str;
				companiesWorkingNumber[i]=companiesList.get(str);
				i++;
			}
    	
    	final CategoryDataset dataset = createDataset();
    	
        final JFreeChart chart = createChart(dataset);
        

        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
       
        BufferedImage img = chart.createBufferedImage(508,204); 

		HttpSession session = request.getSession(); 
		session.setAttribute("img",img); 

		ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection()); 
		String filename = ServletUtilities.saveChartAsPNG(chart,508,204,info,session); 


		session.setAttribute("filename",filename); 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			
			e.printStackTrace();
		}
		        
        return mapping.findForward("succes");
    }
    
    private CategoryDataset createDataset() { 
    	try
    	{
        
        DefaultCategoryDataset dSet=new DefaultCategoryDataset();
        for(int i=0;i<companiesNameList.length;i++)
        	{
        	dSet.addValue(companiesWorkingNumber[i], "Number of Itian works for company", companiesNameList[i]);
        	
        	}
        
        
        return dSet;
		
    	}
    	catch(Exception e)
    	{
    		
    		e.printStackTrace();
    		return null;
    	}
    }

    private JFreeChart createChart(final CategoryDataset dataset) {

        final JFreeChart chart = ChartFactory.createBarChart(
                "ITIan", "Companies", "ITIan Number", dataset,
                PlotOrientation.VERTICAL, true, true, true);
        return chart;
    }

}